function densernet = denser_network(roadnet, nodes, tol, weight)

    % first check of the network is a single connected component 
    connected = conncomp( roadnet);
    idx = connected == mode(connected);
    node_mat = [ nodes(:, 1) nodes(:, 2) ];

    % if it is not fully connected add edge 
    if min(idx)==0

        densernet = roadnet;

        % save down original index
        nodes(:, 3) = find(nodes(:, 1));

        % check which nodes have less than two degrees
        deg_results = rowfun(@(x)check_degree(x, densernet), table(nodes(:, 3)));
        nodes(:, 4) = table2array(deg_results);

        % subset nodes into those that need fixing
        nodes_to_fix = nodes((nodes(:, 4)==1) & (idx'==0), :);

        for j=1:length(nodes_to_fix)
            
            [j length(nodes_to_fix)]

            % get original index of node
            j_o = nodes_to_fix(j, 3);

            eid = outedges(roadnet,j_o);

            % get connected node to exclude from search
            cnxd = roadnet.Edges(eid,:).EndNodes(roadnet.Edges(eid,:).EndNodes~=j_o);

            % find index of next closest node excluding self and already 
            % connected node
             excl2 = [node_mat(j_o, 1) node_mat(j_o, 2)    ];

            if isempty(cnxd)==0
                excl1 = [ node_mat(cnxd(1), 1) node_mat(cnxd(1), 2) ];
                keep_nodes = sum(excl1~=node_mat & excl2~=node_mat,2)==size(node_mat,2);
            else
                keep_nodes = sum(excl2~=node_mat,2)==size(node_mat,2);
            end
            
            node_set = nodes(keep_nodes==1, :);

            % compute distance between node and points 
            haversine_dist = @(x, y)distance(x, y);
            [k, min_dist] = knnsearch(node_set(:, 1:2), excl2, 'Distance', haversine_dist);
            
            [ min_dist ]
            
            % minimum distance should never be 0
            assert(min_dist>0)
            
            % if it is within a tolerance distance away from the 
            % next closest node, add an edge 
            if min_dist < tol
                k_o = nodes(k, 3);

                fprintf('Adding a node')
                [ min_dist ]

                newEdge = table([k_o j_o], [weight], [min_dist], [1], [0], ...
                    'VariableNames',{'EndNodes', 'Weight', 'Distance', 'lanes', 'use'});
                densernet = addedge(densernet, newEdge);

            end        

        end
        
    else
        densernet=roadnet;

    end
   
    
end

  

